package top.archiesean.common.core.domain;

import lombok.Data;
import top.archiesean.common.core.enums.RespStatus;

/**
 * 统一返回值结构
 */
@Data
public class R<T> {
    /**
     * 状态码
     */
    private Integer code;
    /**
     * 响应消息
     */
    private String message;
    /**
     * 响应数据
     */
    private T data;
    /**
     * 成功标记
     */
    private boolean success;

    /**
     * 接口操作成功响应
     *
     * @param <T>  泛型
     * @param data 响应对象
     * @return Response
     */
    public static <T> R<T> success(T data) {
        R<T> res = new R<>();
        res.setCode(RespStatus.SUCCESS.getCode());
        res.setMessage(RespStatus.SUCCESS.getMsg());
        res.setData(data);
        res.setSuccess(true);
        return res;
    }

    /**
     * 接口操作成功响应
     *
     * @return Response
     */
    public static R<?> success() {
        R<?> res = new R<>();
        res.setCode(RespStatus.SUCCESS.getCode());
        res.setMessage(RespStatus.SUCCESS.getMsg());
        res.setData(null);
        res.setSuccess(true);
        return res;
    }

    /**
     * @param <T> 泛型
     * @return response
     */
    public static <T> R<T> success(RespStatus respStatus) {
        R<T> res = new R<>();
        res.setCode(respStatus.getCode());
        res.setMessage(respStatus.getMsg());
        res.setSuccess(true);
        return res;
    }

    /**
     * 接口错误响应
     *
     * @param <T> 泛型
     * @param msg 错误信息
     * @return Response
     */
    public static <T> R<T> fail(String msg) {
        R<T> res = new R<>();
        res.setCode(RespStatus.FAIL.getCode());
        res.setSuccess(Boolean.FALSE);
        res.setMessage(msg);
        return res;
    }

    public static <T> R<T> fail(Integer code, String msg) {
        R<T> res = new R<>();
        res.setCode(code);
        res.setSuccess(Boolean.FALSE);
        res.setMessage(msg);
        return res;
    }

    /**
     * 接口错误响应
     *
     * @param <T> 泛型
     * @param msg 错误信息
     * @return Response
     */
    public static <T> R<T> fail(RespStatus msg) {
        R<T> res = new R<>();
        res.setCode(msg.getCode());
        res.setMessage(msg.getMsg());
        res.setSuccess(Boolean.FALSE);
        return res;
    }
}
